<?php

class Mod_Activity extends CI_Model{
		
	public function getActivity($start, $num, $type){
		$this->db->select('*');
		$this->db->where(field('type'), $type);
        $this->db->limit($num, $start);
		$this->db->order_by(field('id'), 'DESC');
		return $this->db->get(table('activity'));
	}
	
    public function getActivityNum($type){
        $objDb = $this->getAllActivity($type);
        return $objDb->num_rows();
    }
	
    public function getAllActivity($type) {
        $this->db->select('*');
        $this->db->from(table('activity'));
		$this->db->where(field('type'), $type);
		return $this->db->get();
    }
	
    public function getActivityById($id) {
        $this->db->select('*');
        $this->db->where(field('id'),$id);
		$result = $this->db->get(table('activity'));
		$result = $result->result_array();
		return $result[0];
    }
	
	public function insert($image, $title, $description, $type){
		$data = array(
			field('image') => $image,
			field('title') => $title,
			field('description') => $description,
			field('type') => $type
		);			
		$result = $this->db->insert(table('activity'), $data); 
		return $this->db->affected_rows($result);
	}
	
	public function update($id, $image, $title, $description, $type){
		$data = array(
			field('image') => $image,
			field('title') => $title,
			field('description') => $description,
			field('type') => $type
		);			
		if(empty($image)) unset($data[field('image')]);
		
		$result = $this->db->update(table('activity'), $data, field('id') . ' = '. $id); 
		return $this->db->affected_rows($result);
	}
	
    public function delete($id) {
        $this->db->where(field('id'), $id);
        $result = $this->db->delete(table('activity'));
		return $this->db->affected_rows($result);
    }
	
    public function getLastNew() {
        $this->db->select('*1');
        $this->db->from(table('activity'));
		//$this->db->where(field('type'), $type);
		return $this->db->get();
    }
}